package everyday.m20230309;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Q23230324 {

    public static void main(String[] args) {
        StreamChecker streamChecker = new StreamChecker(new String[]{
                "cd", "f", "kl"
        });
        String checkChars = "abcdefghijkl";
        for (char c : checkChars.toCharArray()) {
            System.out.println(streamChecker.query(c));
        }
    }

    static class StreamChecker {

        Set<String> words = new HashSet<>();

        Set<Integer> allSize = new HashSet<>();

        StringBuffer stringBuffer = new StringBuffer();

        public StreamChecker(String[] words) {
            this.words.addAll(Arrays.asList(words));
            for (String word : words) {
                allSize.add(word.length());
            }
        }

        public boolean query(char letter) {
            stringBuffer.append(letter);
            for (Integer integer : allSize) {
                if (stringBuffer.length() < integer) {
                    continue;
                }
                String sub = stringBuffer.substring(stringBuffer.length() - integer,stringBuffer.length());
                if (words.contains(sub)) {
                    return true;
                }
            }
            return false;
        }
    }
}
